<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header th:replace="inc/header"></header>

<body th:with="menu='diseaseCategory'">
<nav th:replace="inc/nav"></nav>

<div class="container-fluid">
  <div class="row">
    <div th:replace="inc/sidebar"></div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h1 class="page-header">
        	疾病发布类型管理
        <div class="pull-right">
          <a class="btn btn-primary btn-sm" id="btnAdd">新增</a>
        </div>
      </h1>
      <table class="table table-striped">
        <thead>
        <tr>
          <th>疾病类型</th>
          <th>编号</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="diseaseCategorys : ${diseaseCategory}">
          <td th:text="${diseaseCategorys.name}"></td>
          <td th:text="${diseaseCategorys.code}"></td>
          <td>
            <a class="btn btn-xs btn-primary btnEdit" th:attr="data-id=${diseaseCategorys.id}">修改</a>
            <a class="btn btn-xs btn-danger btnDelete" th:attr="data-id=${diseaseCategorys.id}">删除</a>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

<div class="modal fade" id="modalSave" tabindex="-1" role="dialog" data-backdrop="static">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
          aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="modalTitle">新增疾病发布类型</h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" id="formSave">
          <input type="hidden" name="jsbh" id="jsbh" th:attr="value=${jsbh}">
          <input type="hidden" name="id" id="id">
          <div class="form-group">
            <label for="code" class="control-label col-md-3">类型名称</label>
            <div class="col-md-9">
              <input type="text" name="name" id="name" class="form-control" required>
            </div>
          </div>
          <div class="form-group">
            <label for="name" class="control-label col-md-3">编号</label>
            <div class="col-md-9">
              <input type="text" name="code" id="code" class="form-control" required>
            </div>
          </div>

        </form>
      </div>
      <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal">取消</button>
        <button class="btn btn-primary" id="btnSave">保存</button>
      </div>
    </div>
  </div>
</div>

<div th:replace="inc/footer"></div>
<script th:inline="javascript">
  var $modal = $('#modalSave');
  var $modalTitle = $('#modalTitle');
  var $form = $('#formSave');

  $form.validate();

  // 新增
  $('#btnAdd').on('click', function () {
    $modalTitle.text('新增疾病发布类型');
    $modal.find('input[type=text], select').val('');
    $modal.find('input[name=id]').val('');
    $modal.modal('show');
  });

  // 保存
  $('#btnSave').on('click', function () {
    if (!$form.valid()) return;
    var data = $form.serializeDict();
    $.ajax({
      url: [[${#httpServletRequest.getContextPath()} +'/D/saveDiseaseCategory']],
      data: data,
      method: 'post',
      success: function () {
        alert('保存成功');
        window.location.reload();
      },
      error: function () {
        alert('保存失败');
      }
    })
  });

  // 删除
  $('.btnDelete').on('click', function () {
    var id = $(this).attr('data-id');
    $.ajax({
      url: [[${#httpServletRequest.getContextPath()} +'/D/delDiseaseCategory']],
      data: {
        id: id
      },
      method: 'post',
      success: function () {
        alert('删除成功');
        window.location.reload();
      },
      error: function () {
        alert('删除失败');
      }
    })
  });

  // 修改
  $('.btnEdit').on('click', function () {
    var id = $(this).attr('data-id');
    $.ajax({
      url: [[${#httpServletRequest.getContextPath()} +'/D/findDiseaseCategoryByid']],
      data: {
        id: id
      },
      success: function (data) {
        $form.find('input:not([type=radio]), select').each(function () {
          $(this).val(data[$(this).attr('name')])
        });
        $modalTitle.text('修改疾病发布类型');
        $modal.modal('show');
      },
      error: function () {
        alert('获取疾病发布类型详情失败');
      }
    })
  })


</script>
</body>
</html>
